
package com.datacoper.remotecommands;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.MessageFormat;

import javax.swing.JButton;
import javax.swing.JOptionPane;

public class GlassfishCommands {

	private File glassfishHome;
	private File asAdminFile;

	private GlassfishCommands(String glassfishHomePath) throws FileNotFoundException {
	  glassfishHome = new File(glassfishHomePath);
	  validateExists(glassfishHome);
	  
	  asAdminFile = new File(glassfishHome, "bin/asadmin.bat");
	  validateExists(asAdminFile);
  }
	
	private void validateExists(File file) throws FileNotFoundException{

	  if(!file.exists()){
	  	throw new FileNotFoundException(MessageFormat.format("Diret�rio {0} n�o encontrado.", file.getPath()));
	  }
	}
	
	public StringBuilder execute(String asAdminCommand) throws IOException{
		
		StringBuilder sb = new StringBuilder();
		
		try {
	    
			String command = "start "+asAdminFile.getAbsolutePath() +" "+ asAdminCommand;
			System.out.println("Command: "+command);	
			Process p = Runtime.getRuntime().exec(command);
	    
	    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

			String line;

			while ((line = reader.readLine()) != null) {
				sb.append(line);
				sb.append("\n");
			}
	    System.out.println("Exit: "+p.exitValue());
			reader.close();
			
    } catch (IOException e) {
	    throw new IOException(MessageFormat.format("Erro ao executar o comando: {0}", asAdminCommand), e);
    }
		
		return sb;
	}
	
	
	public static void main(String[] args) throws IOException {
//	  GlassfishCommands commands = new GlassfishCommands("C:/ProjetosEE/glassfish3.1.2.2/");
//	  
//	  //StringBuilder sb = commands.execute("start-domain producao");
//	  StringBuilder sb = commands.execute("stop-domain producao");
//	  
//	  //StringBuilder sb = commands.execute("list-domains");
//	  
//	  System.out.println(sb);
//	  
//	  System.exit(0);
	  
	  JButton button = new JButton();
	  
	  
	  
	  final Process p = Runtime.getRuntime().exec(new String[]{"brasero", "/home/thiago/dicom.iso"});
	  
	  button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        p.destroy();
      }
    });
	  
	  Thread t = new Thread(new Runnable() {
      
      @Override
      public void run() {
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));

        String line;

        try {
          while ((line = reader.readLine()) != null) {
            System.out.println(line);
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
        
        System.out.println(p.exitValue());
        
      }
    });
	  
	  t.start();
	  
	  JOptionPane.showMessageDialog(null, button);
	  
    
    //System.out.println(p.exitValue());
	  
  }
	
}
